import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

//JDBC进行修改操作
public class JDBCPracticeUpdate {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java107_2?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("12345");
        Connection connection = dataSource.getConnection();

        System.out.println("请输入要更新后的id：");
        int id = scanner.nextInt();
        System.out.println("要更新id的名字：");
        String name = scanner.nextLine();
        //创建sql语句
        String sql = "update student set id = ? where name = ?";
        //预编译
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,id);
        statement.setString(2,name);
        //执行sql语句
        int ret = statement.executeUpdate();
        System.out.println("ret = " + ret);
        //释放必要的资源
        statement.close();
        connection.close();
    }
}
